package net.spleefultimate.commands;

import net.spleefultimate.Messages;
import net.spleefultimate.RegisterManager;
import net.spleefultimate.SpleefGame;
import net.spleefultimate.util.MessageFormatter;

import org.bukkit.entity.Player;

public class SetRewardCommand extends SubCommand
{
  public boolean onCommand(Player player, String[] args)
  {
    if (args.length != 2) {
      return false;
    }

    if (!player.hasPermission("spleefultimate.admin.setreward"))
    {
      player.sendMessage(Messages.getMessage("error-player_nopermission"));

      return true;
    }

    SpleefGame game = getGame(args[0]);
    if (game == null)
    {
      player.sendMessage(MessageFormatter.format(Messages.getMessage("error-game_doesnotexist"), "{ID}", 
        args[0]));

      return true;
    }

    double newReward = game.getReward();
    try
    {
      newReward = Double.valueOf(args[1]).doubleValue();
    }
    catch (NumberFormatException ex)
    {
      player.sendMessage(MessageFormatter.format(Messages.getMessage("error-novalidnumber"), "{NUMBER}", 
        args[1]));

      return true;
    }

    if (RegisterManager.hasMethod())
    {
      game.setReward(newReward);

      player.sendMessage(MessageFormatter.format(Messages.getMessage("message-game_setreward"), "{MONEYAMOUNT}", 
        RegisterManager.getMethod().format(newReward)));
    }

    return true;
  }

  public String getUsage()
  {
    return "/spleef setreward <gameId> <amount>";
  }
}